package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.OrderAddNewParam;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.OrderListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.OrderStandardVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IOrderService;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "4.订单管理")
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private IOrderService service;

    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                             @Valid OrderAddNewParam orderAddNewParam){
        service.addNew(principal,orderAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询订单")
    @ApiOperationSupport(order = 130)
    @GetMapping("/{id:[0-9]+}/select-by-id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "订单id",required = true, dataType = "long")
    })
    public JsonResult selectById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                                 @PathVariable @Range(min = 1,message = "请提供有效的订单id") Long id){
        OrderStandardVO orderStandardVO = service.selectByUserByOrderId(principal, id);
        return JsonResult.ok(orderStandardVO);
    }

    @ApiOperation("根据用户id查询订单列表")
    @ApiOperationSupport(order = 131)
    @GetMapping("/list-by-user-id")
    public JsonResult listByUserId(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal){
        List<OrderListItemVO> list = service.list(principal);
        return JsonResult.ok(list);
    }
}
